#include "ajax.h"

Ajax::Ajax(QObject *parent) :
    QObject(parent)
{
//  nam.setCookieJar(CookieJar::getInstance());
}

Ajax::~Ajax()
{
}

QNetworkReply *Ajax::get(const QString &url, const QMap<QString, QString> &headers, bool compress)
{
  QString auth = "";
  request.setUrl(url);
  request.setRawHeader("Connection", "Keep-Alive");
  request.setRawHeader("Keep-Alive", "timeout=30");
  request.setRawHeader("Accept-Encoding", "none");

  QMap<QString, QString>::const_iterator i = headers.begin();
  while (i != headers.end()) {
    request.setRawHeader(i.key().toUtf8(), i.value().toUtf8());
    i++;
  }

  return nam.get(request);
}

QNetworkReply *Ajax::post(const QString &url, const QMap<QString, QString> &headers, const QString &param)
{
  QString auth = "";
  request.setUrl(url);
  request.setRawHeader("Connection", "Keep-Alive");
  request.setRawHeader("Keep-Alive", "timeout=30");
  request.setRawHeader("Accept-Encoding", "none");

  QMap<QString, QString>::const_iterator i = headers.begin();
  while (i != headers.end()) {
    request.setRawHeader(i.key().toUtf8(), i.value().toUtf8());
    i++;
  }

  return nam.post(request, param.toUtf8());
}

QNetworkReply *Ajax::post(const QString &url, const QMap<QString, QString> &headers, bool compress)
{
  QString auth = "";
  request.setUrl(url);
  request.setRawHeader("Connection", "Keep-Alive");
  request.setRawHeader("Keep-Alive", "timeout=30");
  request.setRawHeader("Accept-Encoding", "none");

  QMap<QString, QString>::const_iterator i = headers.begin();
  while (i != headers.end()) {
    request.setRawHeader(i.key().toUtf8(), i.value().toUtf8());
    i++;
  }

  return nam.post(request, QString("").toUtf8());
}

void Ajax::post(const QString &_url, const QString &param, bool compress)
{
 QString __url = _url;
 QString __param = param;
qDebug() << " Ajax Post " << _url << param;
  request.setUrl(__url);
  request.setRawHeader("Connection", "Keep-Alive");
  request.setRawHeader("Keep-Alive", "timeout=30");
#if 0
  if (compress)
    request.setRawHeader("Accept-Encoding", "gzip");
  else request.setRawHeader("Accept-Encoding", "");
#endif
  request.setRawHeader("Accept-Encoding", "");

  request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
qDebug() << " DEF 0 ";
  QNetworkReply *reply = nam.post(request, __param.toUtf8());
qDebug() << " DEF 1 ";
  connect(reply, SIGNAL(finished()), this, SLOT(nmfinished()));
  connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(nmerror(QNetworkReply::NetworkError)));
qDebug() << " DEF 2 ";
}

void Ajax::get(const QString &_url, bool compress)
{
  request.setUrl(_url);
  request.setRawHeader("Connection", "Keep-Alive");
  request.setRawHeader("Keep-Alive", "timeout=30");
#if 0
  if (compress)
    request.setRawHeader("Accept-Encoding", "gzip");
  else request.setRawHeader("Accept-Encoding", "");
#endif
//  request.setRawHeader("Accept-Encoding", "");

  request.setHeader(QNetworkRequest::ContentTypeHeader, "");

  QNetworkReply *reply = nam.get(request);
  connect(reply, SIGNAL(finished()), this, SLOT(nmfinished()));
  connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(nmerror(QNetworkReply::NetworkError)));
}

void Ajax::nmfinished()
{
  QNetworkReply *reply = qobject_cast<QNetworkReply *>(QObject::sender());

  qDebug() << " Finished0 " << reply->rawHeaderList();
  qDebug() << " Finished1 " << reply->rawHeader("Content-Encoding");

  QByteArray data;
  if (reply->hasRawHeader("Content-Encoding") && reply->rawHeader("Content-Encoding") == "gzip") {
    InflateStream inflate(reply);
    data = inflate.readAll();
  } else {
    data = reply->readAll();
  }

  qDebug() << " Data " << QString::fromUtf8(data);

  emit success(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),
               QString::fromUtf8(data));

  CookieJar::getInstance()->saveCookie();

  reply->disconnect();
  reply->deleteLater();
}

void Ajax::nmerror(QNetworkReply::NetworkError code)
{
  QNetworkReply *reply = qobject_cast<QNetworkReply *>(QObject::sender());

  qDebug() << " NMError " << code;

  qDebug() << " Error0 " << reply->rawHeaderList() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) << code;

  emit error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),
             QString::fromUtf8(reply->readAll()));

  qDebug() << " Error1 " << reply->rawHeaderList() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) << code;

  reply->disconnect();
  reply->deleteLater();
}
